﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WASM.WebSite.Logic.Logging;

namespace WASM.WebSite
{
	// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
	// visit http://go.microsoft.com/?LinkId=9394801

	public class MvcApplication : System.Web.HttpApplication
	{
		public static void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axp/{*pathInfo}");

			//routes.MapRoute(
			//  "Storage",
			//   "Blob|Table/{action}/{storageId}/{storageBoxId}/{itemId}",
			//  new { controller = "Blob", action = "List", storageId = "", storageBoxId = "", itemId = "" }
			//);

			routes.MapRoute(
					"Default",                                              // Route name
					"{controller}/{action}/{id}",                           // URL with parameters
					new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
			);
		}

		protected void Application_Start()
		{
			RegisterRoutes(RouteTable.Routes);
		}

		protected void Application_Error(object sender, EventArgs e)
		{
			Exception error = Server.GetLastError();

			if (error is HttpUnhandledException && error.InnerException != null)
				error = error.InnerException;

			String errorId = Logger.Write(error);
			Server.ClearError();

			if (Request.Url.AbsoluteUri.Contains("/Shared/Error"))
			{
				Response.Write(String.Format("Critical error has occured on error page. Error Id='{0}'", errorId));
				Response.End();
			}
			else
			{
				Response.Redirect("~/Shared/Error/" + errorId);
			}
		}
	}
}